﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NJL.BLL;
using System.Data;
using System.Text.RegularExpressions;
using Royalsoft.Common;

namespace NJL.WebManage.Users
{
    public partial class UserManage : BasePage
    {
        PagedDataSource pds;
        NJL.BLL.NJL_PERSON bll = new BLL.NJL_PERSON();
        DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["search"] = "";
                txtStartDate.Text = "2011-01-01";
                txtEndState.Text = DateTime.Now.ToString("yyyy-MM-dd");
                ViewState["pager"] = "0";
                bind();
            }
        }
        public void bind()
        {
            ds = bll.GetListUserInPerson(strSql());
            txtPage.Text = (Convert.ToInt32(ViewState["pager"]) + 1).ToString();
            pds = new PagedDataSource();
            pds.DataSource = ds.Tables[0].DefaultView;
            pds.AllowPaging = true;
            pds.PageSize = 15;
            pds.CurrentPageIndex = Convert.ToInt32(ViewState["pager"]);
            ViewState["endpage"] = pds.PageCount;
            lbtnFirst.Enabled = true;
            lbtnPrev.Enabled = true;
            lbtnNext.Enabled = true;
            lbtnEnd.Enabled = true;
            if (ViewState["pager"].ToString() == "0")
            {
                lbtnFirst.Enabled = false;
                lbtnPrev.Enabled = false;
            }
            if (Convert.ToInt32(ViewState["pager"]) == pds.PageCount - 1)
            {
                lbtnNext.Enabled = false;
                lbtnEnd.Enabled = false;
            }
            lit.Text = ds.Tables[0].Rows.Count.ToString();
            litAllPage.Text = pds.PageCount.ToString();
            litCurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();
            UserRepeater.DataSource = pds;
            UserRepeater.DataBind();
        }
        string sql = "";
        protected string strSql()
        {
            if (ViewState["search"].ToString() == "ture")
            {
                if (ddlUserType.SelectedValue == "-2")
                {
                    sql = "(a.USER_NAME like '%" + txtKeywords.Text.Trim() + "%' or a.EMAIL like '%" + txtKeywords.Text.Trim() + "%') and a.CREATE_DATE_TIME between '" + txtStartDate.Text.Trim() + "' and '" + txtEndState.Text.Trim() + "'";
                }
                else
                {
                    sql = "a.TYPE_CODE='" + ddlUserType.SelectedValue + "' and (a.USER_NAME like '%" + txtKeywords.Text.Trim() + "%' or a.EMAIL like '%" + txtKeywords.Text.Trim() + "%') and a.CREATE_DATE_TIME between '" + txtStartDate.Text.Trim() + "' and '" + txtEndState.Text.Trim() + "'";
                }
            }
            else
            {
                sql = "1=1";
            }
            return sql;
        }
        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();

        }
        protected void lbtnPrev_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) - 1;
            bind();
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) + 1;
            bind();
        }
        protected void lbtnEnd_Click(object sender, EventArgs e)
        {

            ViewState["pager"] = Convert.ToInt32(ViewState["endpage"]) - 1;
            bind();
        }
        private static Regex RegNumber = new Regex("^[0-9]+$");
        protected void btnRir_Click(object sender, EventArgs e)
        {
            Match mc = RegNumber.Match(txtPage.Text);
            if (mc.Success && txtPage.Text != "0")
            {
                ViewState["pager"] = Convert.ToInt32(txtPage.Text) - 1;
                bind();
            }
        }
        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string[] cbxid = HSelectId.Value.Split(',');
            foreach (string id in cbxid)
            {
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
        protected void UserRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal lit = e.Item.FindControl("litNum") as Literal;
                lit.Text = ((e.Item.ItemIndex + 1) + (Convert.ToInt32(ViewState["pager"]) + 1) * 15 - 15).ToString();
                Literal litUserType = e.Item.FindControl("litUserType") as Literal;
                DataRowView drv = (DataRowView)e.Item.DataItem;
                string typeId = drv["TYPE_CODE"].ToString();
                if (typeId == "-1")
                {
                    litUserType.Text = "未激活";
                }
                else if (typeId == "0")
                {
                    litUserType.Text = "已激活";
                }
                else if (typeId == "2")
                {
                    litUserType.Text = "注册用户";
                }
                else if (typeId == "3")
                {
                    litUserType.Text = "商铺用户";
                }
            }
        }
        protected void UserRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                if (bll.Delete(id))
                {
                    bind();

                }
                else
                {
                    MessageBox.Show(this, "删除失败");
                }
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            ViewState["search"] = "ture";
            bind();
        }

    }
}